package o;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import com.huawei.appgallery.foundation.account.bean.UserSession;
import com.huawei.appgallery.foundation.application.pkgmanage.model.reserve.ReserveDbInfo;
import com.huawei.appgallery.foundation.card.base.bean.BaseDistCardBean;
import com.huawei.appgallery.serverreqkit.api.bean.ResponseBean;
import com.huawei.appgallery.serverreqkit.api.listener.IServerCallBack;
import com.huawei.appmarket.service.installdepend.bean.GetDetailByIdResBean;
import com.huawei.appmarket.service.installdepend.bean.GetDetailByIdResBean$DetailInfoBean;
import com.huawei.appmarket.service.webview.base.jssdk.control.AppStatus;
import com.huawei.appmarket.service.webview.base.jssdk.control.FullAppStatus;
import com.huawei.hms.framework.common.ContainerUtils;
import com.huawei.hms.support.api.entity.pay.HwPayConstant;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant;
import java.util.List;
import o.dwk.h;
import o.ewm.n;

public class ene {

    static class a implements OnCancelListener {
        private AsyLoader ˋ;

        public a(AsyLoader o_cwa) {
            this.ˋ = o_cwa;
        }

        public void onCancel(DialogInterface dialogInterface) {
            if (this.ˋ != null) {
                this.ˋ.ˎ(true);
            }
        }
    }

    static class d implements IServerCallBack {
        private djl ˋ;
        private Activity ˎ;
        private String ॱ;

        public d(Activity activity, String str, djl o_djl) {
            this.ˎ = activity;
            this.ॱ = str;
            this.ˋ = o_djl;
        }

        public void prePostResult(BaseRequest o_cvu, ResponseBean responseBean) {
        }

        public void notifyResult(BaseRequest o_cvu, ResponseBean responseBean) {
            if (this.ˋ != null) {
                this.ˋ.ˏ();
            }
            if (this.ˋ != null && this.ˋ.isShowing()) {
                this.ˋ.dismiss();
                this.ˋ = null;
            }
            GetDetailByIdResBean getDetailByIdResBean = (GetDetailByIdResBean) responseBean;
            if (getDetailByIdResBean.getResponseCode() == 0) {
                List ˏ = getDetailByIdResBean.ˏ();
                if (!CollectCheck.isEmpty(ˏ)) {
                    GetDetailByIdResBean$DetailInfoBean getDetailByIdResBean$DetailInfoBean = (GetDetailByIdResBean$DetailInfoBean) ˏ.get(0);
                    if (getDetailByIdResBean$DetailInfoBean != null) {
                        BaseDistCardBean ˊ = ene.ˎ(getDetailByIdResBean$DetailInfoBean);
                        if (!TextUtils.isEmpty(getDetailByIdResBean$DetailInfoBean.ॱॱ())) {
                            String ॱॱ = getDetailByIdResBean$DetailInfoBean.ॱॱ();
                            int indexOf = ॱॱ.indexOf(63);
                            if (indexOf != -1) {
                                ॱॱ = ॱॱ.substring(0, indexOf + 1);
                            }
                            ˊ.ˏॱ(ॱॱ + this.ॱ);
                        }
                        ene.ˋ(ˊ, this.ˎ);
                    }
                }
            }
        }
    }

    public static void ˋ(Activity activity, String str, String str2, String str3) {
        if (activity == null || TextUtils.isEmpty(str) || TextUtils.isEmpty(str2)) {
            Logger.e("WebDownloadManager", "activity or packageName or url is Empty");
        } else if (doo.ˊ(activity)) {
            HcridSession ˋ = HcridSession.ˋ();
            if (ˋ != null && !TextUtils.isEmpty(str2)) {
                if (str2.indexOf("?") == -1) {
                    if (str2.indexOf(HwPayConstant.KEY_SIGN) == -1) {
                        str2 = str2 + "?sign=" + ˋ.ˊ();
                    }
                } else if (str2.indexOf(ContainerUtils.KEY_VALUE_DELIMITER) == -1 && str2.indexOf("?") == str2.length() - 1) {
                    str2 = str2 + "sign=" + ˋ.ˊ();
                } else if (str2.indexOf(HwPayConstant.KEY_SIGN) == -1) {
                    str2 = str2 + "&sign=" + ˋ.ˊ();
                }
                String ॱ = ept.ॱ(str2, str3);
                int indexOf = ॱ.indexOf(63);
                String str4 = null;
                if (indexOf != -1) {
                    str4 = ॱ.substring(indexOf + 1);
                }
                ˋ(activity, str, str4);
            }
        } else {
            evk.ˋ(activity, activity.getResources().getString(n.ʹˊ), 1).ˋ();
        }
    }

    private static void ˋ(Activity activity, String str, String str2) {
        SessionDownloadTask ˎ = dvt.ˏ().ˎ(str);
        if (ˎ == null) {
            ॱ(activity, str, str2);
            return;
        }
        h hVar = new h();
        hVar.ˊ(activity.getBaseContext());
        hVar.ˏ(ˎ);
        hVar.ˏ(null);
        new dwk().ˏ(true, hVar);
    }

    private static void ॱ(Activity activity, String str, String str2) {
        djl o_djl = new djl(activity);
        o_djl.ˋ(ContentManager.getInstant().getContext().getString(n.ˊߴ));
        o_djl.ˎ(500);
        o_djl.setOnCancelListener(new a(cfw.ˎ(new eaj(str), new d(activity, str2, o_djl))));
    }

    private static void ˋ(BaseDistCardBean baseDistCardBean, final Activity activity) {
        enb o_enb = new enb(activity);
        o_enb.setParam(baseDistCardBean);
        o_enb.ॱ();
        o_enb.onClick(null);
        final Object ॱˌ = baseDistCardBean.ॱˌ();
        if (TextUtils.isEmpty(ॱˌ)) {
            Logger.ˋ("WebDownloadManager", "WebView download icon url is null!");
        } else {
            activity.runOnUiThread(new Runnable() {
                public void run() {
                    eqx.ॱ(activity, ॱˌ, null);
                }
            });
        }
    }

    private static BaseDistCardBean ˎ(GetDetailByIdResBean$DetailInfoBean getDetailByIdResBean$DetailInfoBean) {
        BaseDistCardBean baseDistCardBean = new BaseDistCardBean();
        baseDistCardBean.ˉ(getDetailByIdResBean$DetailInfoBean.ॱˊ());
        baseDistCardBean.ˏˎ(getDetailByIdResBean$DetailInfoBean.ʻ());
        baseDistCardBean.ˋॱ(getDetailByIdResBean$DetailInfoBean.ˋॱ());
        baseDistCardBean.ˋᐝ(getDetailByIdResBean$DetailInfoBean.ᐝ());
        try {
            baseDistCardBean.ॱ(Long.parseLong(getDetailByIdResBean$DetailInfoBean.ʽ()));
        } catch (NumberFormatException e) {
            Logger.e("WebDownloadManager", "NumberFormatException:getSize_" + e.getMessage());
        }
        try {
            baseDistCardBean.ˋॱ(Integer.parseInt(getDetailByIdResBean$DetailInfoBean.ˏ()));
        } catch (NumberFormatException e2) {
            Logger.e("WebDownloadManager", "NumberFormatException:getTargetSDK_" + e2.getMessage());
        }
        baseDistCardBean.ॱ(getDetailByIdResBean$DetailInfoBean.ͺ());
        baseDistCardBean.ˋ(getDetailByIdResBean$DetailInfoBean.ˊॱ());
        baseDistCardBean.ॱᐝ(getDetailByIdResBean$DetailInfoBean.ˏॱ());
        baseDistCardBean.ॱˋ(getDetailByIdResBean$DetailInfoBean.ॱˎ());
        baseDistCardBean.ˏॱ(getDetailByIdResBean$DetailInfoBean.ॱॱ());
        baseDistCardBean.ˏ(getDetailByIdResBean$DetailInfoBean.ʼ());
        baseDistCardBean.ॱˊ(getDetailByIdResBean$DetailInfoBean.ॱ());
        baseDistCardBean.ˎ(getDetailByIdResBean$DetailInfoBean.ˎ());
        baseDistCardBean.ॱ(getDetailByIdResBean$DetailInfoBean.ॱᐝ());
        baseDistCardBean.ͺ(getDetailByIdResBean$DetailInfoBean.ʻॱ());
        return baseDistCardBean;
    }

    public static String ˋ(String str) {
        AppStatus appStatus = new AppStatus();
        appStatus.ˊ(str);
        if (TextUtils.isEmpty(str)) {
            return ˋ(appStatus, -2);
        }
        SessionDownloadTask ॱ = ॱ(str);
        if (ॱ == null) {
            return ˋ(appStatus, ˋ(str, ((cdg) cdb.ॱ(cdg.class)).ˋ(str)));
        }
        appStatus.ˊ(ॱ.ʼॱ());
        return ˋ(appStatus, ˊ(ॱ));
    }

    private static String ˋ(AppStatus appStatus, int i) {
        String str = "";
        appStatus.ˏ(i);
        try {
            str = appStatus.toJson();
        } catch (IllegalAccessException e) {
            Logger.ˎ("WebDownloadManager", "appStauts error");
            if (Logger.ˎ()) {
                Logger.ˏ("WebDownloadManager", "getAppStatus:" + str);
            }
            return str;
        } catch (IllegalArgumentException e2) {
            Logger.ˎ("WebDownloadManager", "appStauts error");
            if (Logger.ˎ()) {
                Logger.ˏ("WebDownloadManager", "getAppStatus:" + str);
            }
            return str;
        }
        if (Logger.ˎ()) {
            Logger.ˏ("WebDownloadManager", "getAppStatus:" + str);
        }
        return str;
    }

    private static int ˋ(String str, int i) {
        if (i == 0) {
            if (efg.ˋ().ˏ(str)) {
                return 14;
            }
            return 0;
        } else if (-2 != i) {
            return i;
        } else {
            if (efg.ˋ().ˏ(str)) {
                return 14;
            }
            return -2;
        }
    }

    private static int ˊ(SessionDownloadTask sessionDownloadTask) {
        switch (sessionDownloadTask.ˈ()) {
            case -1:
                return 9;
            case 1:
            case 2:
            case 8:
                return 6;
            case 5:
                return -2;
            case 6:
                return 7;
            case 7:
                return 8;
            default:
                return 5;
        }
    }

    public static boolean ˊ(String str) {
        return ॱ(str) != null;
    }

    private static SessionDownloadTask ॱ(String str) {
        return dvt.ˏ().ˋ(str);
    }

    public static void ˊ(final Activity activity, String str) {
        if (TextUtils.isEmpty(str)) {
            Logger.e("WebDownloadManager", "can not cancel Download,packageName is Empty.");
            return;
        }
        cnz o_cnz = cnz.ॱ;
        fhz ˋ = fho.ˏ().ˋ("PackageManager");
        if (ˋ != null) {
            o_cnz = ((coa) ˋ.ॱ(coa.class)).ˏ(str);
        }
        if (cnz.ˊ == o_cnz || cnz.ˏ == o_cnz) {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                public void run() {
                    evk.ˏ(activity, n.ॱˋ, 0).ˋ();
                }
            });
        } else {
            ˎ(str);
        }
    }

    private static void ˎ(String str) {
        ((cdf) cdb.ॱ(cdf.class)).ॱ(str);
        dsv.ˏ(str);
        new dwk().ॱ(str);
        Logger.ˎ("WebDownloadManager", "cancle download:" + str);
    }

    public static void ˎ(Activity activity, String str) {
        if (TextUtils.isEmpty(str)) {
            Logger.e("WebDownloadManager", "can not pause Download,packageName is Empty.");
        } else {
            ʼ(str);
        }
    }

    private static void ʼ(String str) {
        SessionDownloadTask ˋ = dvt.ˏ().ˋ(str);
        if (ˋ != null) {
            dvt.ˏ().ˏ(ˋ.ʾ());
        }
    }

    public static void ˏ(Activity activity, String str, efe o_efe) {
        int i = n.ʽˋ;
        dgh.onEvent(new o.dge.d(ContentManager.getInstant().getContext(), i).ˏ(str + HiAnalyticsConstant.REPORT_VAL_SEPARATOR + UserSession.getInstance().getUserId()).ˋ());
        efb.ॱ().ˋ(activity, str, null, o_efe);
    }

    public static void ˏ(String str) {
        ReserveDbInfo ॱ = efg.ˋ().ॱ(str);
        FullAppStatus fullAppStatus = new FullAppStatus();
        fullAppStatus.ˊ(str);
        fullAppStatus.ˏ(ॱ == null ? 0 : 1);
        fullAppStatus.appType_ = 3;
        enf.ˋ().ˊ(fullAppStatus);
    }

    public static void ॱ(Activity activity, String str, cks o_cks) {
        if (TextUtils.isEmpty(str)) {
            Logger.e("WebDownloadManager", "packageName is empty.");
            return;
        }
        cku ˏ = cku.ˏ(activity, cku.class, null, activity.getResources().getString(n.ͺˏ));
        ˏ.ˎ(activity);
        ˏ.ॱ(-1, activity.getString(n.ͺˎ));
        ˏ.ॱ(-2, activity.getString(n.ˏͺ));
        ˏ.ॱ(o_cks);
    }

    public static void ˎ(Activity activity, String str, efe o_efe) {
        efb.ॱ().ˏ(activity, str, null, o_efe);
    }
}
